package com.ruoyi.project.drug.service.impl;

import com.ruoyi.project.drug.domain.DruDrug;
import com.ruoyi.project.drug.domain.DruPurchaseDrug;
import com.ruoyi.project.drug.mapper.DruPurchaseDrugMapper;
import com.ruoyi.project.drug.mapper.DruPurchaseMapper;
import com.ruoyi.project.drug.service.DruPurchaseDrugService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
public class DruPurchaseDrugServiceImpl implements DruPurchaseDrugService {

    @Autowired
    private DruPurchaseDrugMapper druPurchaseDrugMapper;


    @Autowired
    private DruPurchaseMapper druPurchaseMapper;


    /**
     * 根据添加的药品信息集合对订单药品信息进行修改新增
     *
     * @param drugList  药品信息集合
     * @return  返回1成功
     */
    @Override
    public int suboptimal(List<DruPurchaseDrug> drugList) {

        DruPurchaseDrug dp = drugList.get(0);
        int i1 = druPurchaseDrugMapper.deleteDrug(dp.getDpid());

        int flag = 1;
        for (DruPurchaseDrug druPurchaseDrug:drugList) {
            int i = druPurchaseDrugMapper.addDrug(druPurchaseDrug);
            if (i != 1){
                flag = 0;
            }
        }

        if (dp.getDpdid() == 1){
            int i = druPurchaseMapper.updateState(dp.getDpid(), "1");
        }else if (dp.getDpdid() == 0){
            int i = druPurchaseMapper.updateState(dp.getDpid(), "4");
        }

        return flag;
    }
}
